package x10.utilities;

import java.io.Serializable;

import x10.enums.InterfaceMessage;

/**
 *  ChunkOfBytes class - utility class for passing byte arrays between
 *  factory classes.
 *
 *  Pretty descriptive name, right?
 *
 *  Normally, you can simply call the .size() method of a byte array, but
 *  the USB Bulk Read method of libusb returns a byte array with the size
 *  being system dependent.  Libusb also returns an int value telling you
 *  how many bytes it put in the array.  This class captures those two
 *  pieces and joins them in a portable object along with a boolean isEmpty
 *  member for convenience.
 *
 *
 *  @author Denny Sheirer
 *  @date   20110124
 */

public class ChunkOfBytes implements Serializable{
    static final long serialVersionUID = 1L;
    byte[] bytes;
    int length;
    boolean empty;
    boolean interfaceRequest;
    InterfaceMessage interfaceMessage;
    int interfaceReceiveCycles;

    /**
     *  ChunkOfBytes class - constructor
     *  Creates an empty class with empty=true;
     */
    public ChunkOfBytes(){
        empty = true;
        interfaceRequest = false;
    }

    /**
     * Fetches the byte array from this object
     * @return byte[]
     */
    public byte[] getBytes() {
        return bytes;
    }

    /**
     * Set the byte array for this object
     * @param bytes - byte array
     */
    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    /**
     * Fetches the length/number of bytes contained in the bytes byte array
     * @return length - number of bytes in byte array
     */
    public int getLength() {
        return length;
    }

    /**
     * Set the number of bytes in the byte array
     * @param length - number of bytes in byte array
     */
    public void setLength(int length) {
        this.length = length;
    }

    /**
     * @return empty - indicates whether this object has bytes and length
     */
    public boolean isEmpty() {
        return empty;
    }

    /**
     * Set boolean value indicating if object is empty or contains bytes and length
     * @param empty
     */
    public void setEmpty(boolean empty) {
        this.empty = empty;
    }

    /**
     * Single byte identifying an interface request
     * @return byte value representing the interface request type
     */
    public InterfaceMessage getInterfaceMessage() {
        return interfaceMessage;
    }

    /**
     * Single byte identifying the interface request
     * @param interfaceMessage
     */
    public void setInterfaceMessage(InterfaceMessage interfaceMessage) {
        this.interfaceMessage = interfaceMessage;
    }

    /**
     * Flag to identify a chunkOfBytes as an interface request
     * @return
     */
    public boolean isInterfaceRequest() {
        return interfaceRequest;
    }

    /**
     * Flag to identify a chunkOfBytes as an interface request
     */
    public void setInterfaceRequest(boolean interfaceRequest) {
        this.interfaceRequest = interfaceRequest;
    }

    /**
     * When an interface message request is transmitted, there may
     * be more than one receive cycle needed to receive all of the
     * message bytes from the interface.  Set this to the number of
     * expected receive cycles to catch the entire message.
     * @return count of receive cycles
     */
    public int getInterfaceReceiveCycles() {
        return interfaceReceiveCycles;
    }

    /**
     * When an interface message request is transmitted, there may
     * be more than one receive cycle needed to receive all of the
     * message bytes from the interface.  Set this to the number of
     * expected receive cycles to catch the entire message.
     */
    public void setInterfaceReceiveCycles(int interfaceReceiveCycles) {
        this.interfaceReceiveCycles = interfaceReceiveCycles;
    }

    /**
     * Utility method converting byte array to hex string
     * @return String with hex representation of bytes in byte array
     */
    @Override
    public String toString(){
        return Utils.byteArrayToString(bytes, length);
    }
}
